package be.djdb.drupal.wizards;
import java.io.InputStream;
import java.net.URL;
import java.util.ResourceBundle;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExecutableExtension;
import org.eclipse.core.runtime.IPluginDescriptor;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.wizards.newresource.BasicNewProjectResourceWizard;

import be.djdb.custom.GZException;
import be.djdb.custom.WizardNewProjectwizard;
import be.djdb.custom.WorkspaceFolders;
import be.djdb.generators.ATemplate;
import be.djdb.utils.AResourceFile;
public class CreateDrupalproject extends WizardNewProjectwizard implements INewWizard, IExecutableExtension{
	protected IConfigurationElement configurationElement;

	private WorkspaceFolders wks;
	//!!!DEBUG!!! Nulpointer exception if not exist
	//public static ImageDescriptor ICON_HOME = DRUPAL_ICONS.getimg("Naamloos16.gif");
	/*
	 * new Projectresources("drupalplugin");
	 **/
	public CreateDrupalproject() {
	            super(); 
		//super("drupalplugin",ICON_HOME);
	}
	public boolean performFinish(){
		try{
			getContainer().run(false, true, super.GetprojectCreator());
			ATemplate template =new ATemplate();
			System.out.println(newProject.getFullPath().toString());
			//wks.getClass().
			//ResourcesPlugin.getWorkspace().getRoot().getFullPath()
			template.setloc(newProject.getFullPath().makeAbsolute().toString());
			try {
				System.out.println("unpack /lib/lib/testfile.dat");
				System.out.println();
				ResourceBundle.getBundle("com.example.hello.HelloPluginResources");//.getin;
				//new AResourceFile().printInputStream(FileLocator.openStream("Drupalplugin", "/lib/lib/testfile.dat", null));
				System.out.println("unpack /lib/testfile.dat");
				new AResourceFile().printInputStream(new AResourceFile().takeajarfilestream("./lib/testfile.dat"));
				System.out.println("unpack /lib/Presets/versions/drupal-6.16.tar.gz");
				InputStream is =new AResourceFile().takeajarfilestream("/lib/Presets/versions/drupal-6.16.tar.gz")  ;
				template.getfromGZ(is);
				wks = new WorkspaceFolders(newProject,null);
				wks.CreatIfNotexist(new String[]{"themes","modules","includes"});
			} catch (GZException e) {
				System.out.println("unpack");
				wks = new WorkspaceFolders(newProject,null);
				wks.CreatIfNotexist(new String[]{"themes","modules","includes"});
				e.printStackTrace();
			}
		}catch(Exception e){
			return false;
		}
		BasicNewProjectResourceWizard.updatePerspective(configurationElement);
		selectAndReveal(newProject);
		return true;
	}
	public void setInitializationData(IConfigurationElement config,String propertyName, Object data) throws CoreException {
		configurationElement = config;
	}

}
